﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGinnBPM.Lib.Interfaces;
using System.Configuration;

namespace NGinnBPM.Lib.Util
{
    public class AppConfigProvider : IConfigParameterProvider
    {
        public string GetString(string paramName)
        {
            return GetString(paramName, null);
        }

        public string GetString(string paramName, string defaultValue)
        {
            string prf = "ConnectionString.";
            if (paramName.StartsWith(prf))
            {
                string name = paramName.Substring(prf.Length);
                var css = ConfigurationManager.ConnectionStrings[name];
                return css != null ? css.ConnectionString : defaultValue;
            }
            else
            {
                var v = ConfigurationManager.AppSettings[paramName];
                return v == null ? defaultValue : v;
            }
        }

        public int GetInt(string paramName, int defaultValue)
        {
            return Int32.Parse(GetString(paramName, defaultValue.ToString()));
        }

        public double GetDouble(string paramName, double defaultValue)
        {
            return Double.Parse(GetString(paramName, defaultValue.ToString()));
        }

        public bool GetBool(string paramName, bool defVal)
        {
            return Boolean.Parse(GetString(paramName, defVal.ToString()));
        }

        public IList<string> GetParameterNames()
        {
            List<string> lst = new List<string>();
            foreach (string k in ConfigurationManager.AppSettings.Keys)
                lst.Add(k);
            foreach (ConnectionStringSettings cs in ConfigurationManager.ConnectionStrings)
            {
                lst.Add("ConnectionString." + cs.Name);
            }
            return lst;
        }

        public void Set(string param, string val)
        {
            throw new NotImplementedException();
        }

        public void Set(string param, int val)
        {
            throw new NotImplementedException();
        }

        public void Set(string param, double val)
        {
            throw new NotImplementedException();
        }

        public void Set(string param, bool val)
        {
            throw new NotImplementedException();
        }
    }
}
